package com.green.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * 
 * @author google 报表时间处理
 * 
 */
public class ConstantsDate {
	// 年
	public static final int YEAR = 1;
	// 月
	public static final int MONTH = 2;
	// 季度
	public static final int SEASON = 3;
	// 周
	public static final int WEEK = 4;
	// 返回格式为时间类型
	public static final int RETURNDATE = 1;
	// 返回格式为字符串
	public static final int RETURNSTRING = 2;

	public static final String YEARMONTH = "yyyyMM";

	/**
	 * 计算近1个月
	 * 
	 * @return
	 */
	@SuppressWarnings("static-access")
	public List<Map<String, Object>> getDay(int count) {
		List<Map<String, Object>> dayList = new ArrayList<Map<String, Object>>();
		DateFormat df = DateFormat.getDateInstance();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(new Date());
		for (int i = 0; i < count; i++) {
			Map<String, Object> dayTime = new HashMap<String, Object>();
			gc.add(5, -1);
			gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE));
			dayTime.put("dateEnd", df.format(gc.getTime()));
			dayTime.put("dateFrom", df.format(gc.getTime()));
			dayList.add(dayTime);
		}
		return dayList;
	}

	/**
	 * 计算近一年
	 * 
	 * @return
	 */
	@SuppressWarnings("static-access")
	public List<Map<String, Object>> getMouth(int count) {
		List<Map<String, Object>> dayList = new ArrayList<Map<String, Object>>();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(new Date());
		for (int i = 0; i < count; i++) {
			Map<String, Object> dayTime = new HashMap<String, Object>();
			int start_month = gc.get(gc.MONTH);
			int years_value = gc.get(gc.YEAR);

			int start_days = 1;
			int end_days = getLastDayOfMonth(years_value, start_month);
			// 处理跨年
			if (start_month == 0) {
				years_value = years_value - 1;
				start_month = 12;
				start_days = 1;
				end_days = 31;
			}
			dayTime.put("dateFrom", years_value + "-" + start_month + "-"
					+ start_days);
			dayTime.put("dateEnd", years_value + "-" + start_month + "-"
					+ end_days);
			gc.add(2, -1);

			dayList.add(dayTime);
		}
		return dayList;
	}

	/**
	 * 获取指定周数的 周一 周日
	 * 
	 * @param count周数
	 * @param params
	 * @return
	 */
	@SuppressWarnings("static-access")
	public List<Map<String, Object>> DailyHouseWeek(int count) {
		List<Map<String, Object>> weekList = new ArrayList<Map<String, Object>>();
		// 本周的星期天
		DateFormat df = DateFormat.getDateInstance();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(this.getMondayOFWeek());
		// 本周
		Map<String, Object> weekTimeone = new HashMap<String, Object>();
		weekTimeone.put("dateFrom", df.format(this.getMondayOFWeek()));
		weekTimeone.put("dateEnd", df.format(this.getCurrentWeekday()));
		weekList.add(weekTimeone);
		// 获取最近30周的 周一 和周日
		for (int i = 1; i < count; i++) {
			Map<String, Object> weekTime = new HashMap<String, Object>();
			gc.add(5, -1);
			gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE));
			weekTime.put("dateEnd", df.format(gc.getTime()));
			gc.add(5, -6);
			gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE));
			weekTime.put("dateFrom", df.format(gc.getTime()));
			weekList.add(weekTime);
		}
		return weekList;
	}

	/**
	 * 计算近5年 按季度分组 为 25组 根据这25组的时间段进行查询
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSeason(int count) {
		List<Map<String, Object>> seasonList = new ArrayList<Map<String, Object>>();
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
		String years = dateFormat.format(date);
		int years_value = Integer.parseInt(years);
		// 获取5年内 4个季度 共20个季度 季度的开始日期 结束日期
		for (int i = 0; i < count; i++) {
			this.getThisSeasonTime(2, years_value, seasonList);// 春
			this.getThisSeasonTime(5, years_value, seasonList);// 夏
			this.getThisSeasonTime(8, years_value, seasonList);// 秋
			this.getThisSeasonTime(11, years_value, seasonList);// 冬
			years_value = years_value - 1;
		}
		return seasonList;
	}

	/**
	 * 根据月份获取季度
	 * 
	 * @param month月份
	 * @return
	 */
	public void getThisSeasonTime(int month, int years_value,
			List<Map<String, Object>> seasonList) {
		// 4个季度的划分
		int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
		int season = 1;
		if (month >= 1 && month <= 3) {
			season = 1;
		}
		if (month >= 4 && month <= 6) {
			season = 2;
		}
		if (month >= 7 && month <= 9) {
			season = 3;
		}
		if (month >= 10 && month <= 12) {
			season = 4;
		}
		int start_month = array[season - 1][0];
		int end_month = array[season - 1][2];

		int start_days = 1;
		int end_days = getLastDayOfMonth(years_value, end_month);

		Map<String, Object> seasonTime = new HashMap<String, Object>();
		seasonTime.put("dateFrom", years_value + "-" + start_month + "-"
				+ start_days);
		seasonTime.put("dateEnd", years_value + "-" + end_month + "-"
				+ end_days);
		seasonList.add(seasonTime);
	}

	/**
	 * 获取某年某月的最后一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 最后一天
	 */
	public static int getLastDayOfMonth(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			return 30;
		}
		if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		}
		return 0;
	}

	/**
	 * 是否闰年
	 * 
	 * @param year
	 *            年
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	// 获得本周星期日的日期
	public Date getCurrentWeekday() {
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		return currentDate.getTime();
	}

	// 获得本周一的日期
	public Date getMondayOFWeek() {
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		return currentDate.getTime();
	}

	// 获得当前日期与本周日相差的天数
	public int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}

	/**
	 * 得到出入格式的季度
	 * 
	 * @param returnFormat
	 * @return
	 */
	public static Map<String, Object> getCurSeason(String returnFormat) {
		return getSeasonTime(new Date(), returnFormat, 0, RETURNSTRING);
	}

	public static Map<String, Object> getUpSeason(String returnFormat) {

		return getSeasonTime(new Date(), "yyyyMMdd", 1,
				ConstantsDate.RETURNSTRING);
	}

	public static Date getUpMonth() {
		GregorianCalendar gc = (GregorianCalendar) GregorianCalendar
				.getInstance();
		gc.setFirstDayOfWeek(Calendar.MONDAY);
		gc.setMinimalDaysInFirstWeek(3);
		gc.setTime(new Date());
		gc.add(Calendar.MONTH, -1);
		
		
		return gc.getTime();
	}

	public static Map<String, Object> getCurMonthFromDate(String returnFormat) {
		return getFromToEndDate(MONTH, new Date(), returnFormat);
	}

	public static Map<String, Object> getCurSeasonFromDate(String returnFormat) {
		return getFromToEndDate(SEASON, new Date(), returnFormat);
	}

	public static Map<String, Object> getCurWeekFromDate(String returnFormat) {
		return getFromToEndDate(WEEK, new Date(), returnFormat);
	}

	public static Map<String, Object> getCurMonthList(String returnFormat) {
		return getDateList(MONTH, 12, new Date(), "yyyyMM");
	}

	public static Map<String, Object> getCurSeasonList() {
		return getDateList(SEASON, 20, new Date(), "yyyyMM");
	}

	public static Map<String, Object> getCurWeekList() {
		return getDateList(WEEK, 30, new Date(), "yyyyMM");
	}

	/**
	 * 获得传入时间与返回时间格式的季度年月,注意，如果要返回dateFrom，我统一成了1号,dateEnd为当月最后一天。
	 * 
	 * @param date
	 * @param returnFormat
	 * @param count
	 *            往上推几个季度
	 * @return
	 */
	public static Map<String, Object> getSeasonTime(Date date,
			String returnFormat, int count, int returnType) {
		Map<String, Object> dateMap = new HashMap<String, Object>();
		GregorianCalendar gc = (GregorianCalendar) GregorianCalendar
				.getInstance();
		gc.setFirstDayOfWeek(Calendar.MONDAY);
		gc.setMinimalDaysInFirstWeek(3);
		gc.setTime(date);
		// 获得月份
		int month = gc.get(Calendar.MONTH);
		gc.set(Calendar.DATE, 1);// 为了在月份变动时不受传入时间的月份天数影响。
		SimpleDateFormat format = new SimpleDateFormat(returnFormat);

		if (month >= 0 && month <= 2) {
			gc.set(Calendar.MONTH, 0);
		}
		if (month >= 3 && month <= 5) {
			gc.set(Calendar.MONTH, 3);
		}
		if (month >= 6 && month <= 8) {
			gc.set(Calendar.MONTH, 6);
		}
		if (month >= 9 && month <= 11) {
			gc.set(Calendar.MONTH, 9);
		}
		gc.add(Calendar.MONTH, -count * 3);
		if (returnType == RETURNSTRING)
			dateMap.put("dateFrom", format.format(gc.getTime()));
		else if (returnType == RETURNDATE)
			dateMap.put("dateFrom", gc);
		gc.add(Calendar.MONTH, 3);
		gc.add(Calendar.DATE, -1);
		if (returnType == RETURNSTRING)
			dateMap.put("dateEnd", format.format(gc.getTime()));
		else if (returnType == RETURNDATE)
			dateMap.put("dateEnd", gc);
		return dateMap;
	}

	public static Map<String, Object> getFromToEndDate(int type, Date date,
			String returnFormat) {
		Map<String, Object> dateMap = new HashMap<String, Object>();
		GregorianCalendar gc = (GregorianCalendar) GregorianCalendar
				.getInstance();
		gc.setFirstDayOfWeek(Calendar.MONDAY);
		gc.setMinimalDaysInFirstWeek(3);
		gc.setTime(date);
		if (type != WEEK)
			gc.set(Calendar.DATE, 1);// 为了在月份变动时不受传入时间的月份天数影响。
		SimpleDateFormat format = new SimpleDateFormat(returnFormat);
		dateMap.put("dateEnd", format.format(date));
		// 以月度计算时间，近一年
		if (type == MONTH) {
			gc.add(Calendar.MONTH, -11);
			dateMap.put("dateFrom", format.format(gc.getTime()));
		}
		// 以季度计算时间，数据为近5年
		if (type == SEASON) {
			gc.add(Calendar.MONTH, -19 * 3);
			String dateFrom = (String) getSeasonTime(gc.getTime(),
					returnFormat, 0, RETURNSTRING).get("dateFrom");
			dateMap.put("dateFrom", dateFrom);
		}
		// 已周度计算时间,数据为近30周
		if (type == WEEK) {
			gc.add(Calendar.WEEK_OF_YEAR, -29);
			dateMap.put("dateFrom", format.format(gc.getTime()));
		}

		return dateMap;
	}

	public static Map<String, Object> getDateList(int type, int count,
			Date date, String returnFormat) {
		Map<String, Object> dateMap = new LinkedHashMap<String, Object>();
		GregorianCalendar gc = (GregorianCalendar) GregorianCalendar
				.getInstance();
		gc.setMinimalDaysInFirstWeek(3);
		gc.setFirstDayOfWeek(Calendar.MONDAY);

		gc.setTime(date);
		SimpleDateFormat format = new SimpleDateFormat(returnFormat);
		if (type != WEEK)
			gc.set(Calendar.DATE, 1);// 为了在月份变动时不受传入时间的月份天数影响。

		// 以月度计算时间，近一年
		if (type == MONTH) {

			gc.add(Calendar.MONTH, 1 - count);
			for (int i = 0; i < count; i++) {
				dateMap.put(format.format(gc.getTime()), 0);
				String t = format.format(gc.getTime());
				gc.add(Calendar.MONTH, 1);
			}
		}
		if (type == SEASON) {
			gc.add(Calendar.MONTH, (1 - count) * 3);
			gc = (GregorianCalendar) getSeasonTime(gc.getTime(), returnFormat,
					0, RETURNDATE).get("dateFrom");
			String dateFrom = null;
			int month = 0;
			for (int i = 0; i < count; i++) {
				dateFrom = gc.get(Calendar.YEAR) + "Q";
				month = gc.get(Calendar.MONTH);
				if (month >= 0 && month <= 2)
					dateFrom += 1;
				if (month >= 3 && month <= 5)
					dateFrom += 2;
				if (month >= 6 && month <= 8)
					dateFrom += 3;
				if (month >= 9 && month <= 11)
					dateFrom += 4;

				dateMap.put(dateFrom, 0);
				gc.add(Calendar.MONTH, 3);
			}
		}
		if (type == WEEK) {
			// 已周度计算时间,数据为近30周,包括所在的周
			gc.add(Calendar.WEEK_OF_YEAR, 1 - count);
			String dateFrom = null;
			for (int i = 0; i < count; i++) {
				dateFrom = gc.get(Calendar.YEAR) + "W"
						+ gc.get(Calendar.WEEK_OF_YEAR);
				dateMap.put(dateFrom, 0);
				gc.add(Calendar.WEEK_OF_YEAR, 1);
			}
		}

		return dateMap;
	}
	/**
	 *季度
	 * 
	 * @param month月份
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getThisSeasonTime() {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(new Date());
		int month=gc.get(gc.MONTH)+1;
		
		String season=gc.get(gc.YEAR)+"";	
		String seasons=gc.get(gc.YEAR)-1+"";	

		if (month >= 1 && month <= 3) {
			season = seasons+"年第四季度";
		}
		if (month >= 4 && month <= 6) {
			season = season+"年第一季度";
		}
		if (month >= 7 && month <= 9) {
			season = season+"年第二季度";
		}
		if (month >= 10 && month <= 12) {
			season = season+"年第三季度";
		}
		
		return season;

	}
}
